package BLL;

import com.kook.ball.util.*;
import com.kook.ball.security.*;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.sql.DataSource;
import javax.annotation.Resource;

/**
 * Servlet implementation class register
 */
@WebServlet("/register")
public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 @Resource(name = "jdbc/security")
     private DataSource jdbcsecurity;
     private Connection conn;
     public void init() {
           try {
                  conn = jdbcsecurity.getConnection();
           		} 
           catch (Exception ex) 
           		{
                  System.out.println(ex);
           		} 
           }
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	
    	PrintWriter out = response.getWriter();
    	response.setContentType("text/html;charset=UTF-8");
    try{
    	String username=request.getParameter("username");
    	String password=request.getParameter("password");
    	String telephone=request.getParameter("telephone");
    	String email=request.getParameter("email");
    	
    	if(!(username.equals("") && password.equals("") && telephone.equals("") && email.equals(""))){
    	PreparedStatement pst = null;
    	//salt
       HashingProvider rs=new HashingProvider();
 	   String salt=rs.getSalt();
 	  
 	   //hash
 	   
 	    String hash = rs.hashSHA256WithSalt(password, salt);
 	  
 	    //String sql = "INSERT INTO Login VALUES (?,?,?,?,?,?)" ;
 	    String sql = "INSERT INTO Login VALUES (?,?,?,?,?)" ;
		pst = conn.prepareStatement(sql);
		pst.setString(1, username);
		pst.setString(2, salt);
		//pst.setString(3, password);
		pst.setString(3, telephone);
		pst.setString(4, hash);
		pst.setString(5, email);
		pst.executeUpdate();
 	    response.sendRedirect("index.html");
    	}
    	else
    		{
    		  response.sendRedirect("Error.html");
    		}
 	  
 	  //out.println(sql);
     }
    catch (Exception e) {
        e.printStackTrace();
    }
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
